package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.HistoryRecordsBo;
import com.bigdata.service.domain.vo.HistoryRecordsVo;
import com.bigdata.service.domain.HistoryRecords;
import com.bigdata.service.mapper.HistoryRecordsMapper;
import com.bigdata.service.service.IHistoryRecordsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地图预警Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@Service
public class HistoryRecordsServiceImpl implements IHistoryRecordsService {

    private final HistoryRecordsMapper baseMapper;

    /**
     * 查询地图预警
     */
    @Override
    public HistoryRecordsVo queryById(Long recordId){
        return baseMapper.selectVoById(recordId);
    }

    /**
     * 查询地图预警列表
     */
    @Override
    public TableDataInfo<HistoryRecordsVo> queryPageList(HistoryRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HistoryRecords> lqw = buildQueryWrapper(bo);
        Page<HistoryRecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地图预警列表
     */
    @Override
    public List<HistoryRecordsVo> queryList(HistoryRecordsBo bo) {
        LambdaQueryWrapper<HistoryRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HistoryRecords> buildQueryWrapper(HistoryRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HistoryRecords> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), HistoryRecords::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), HistoryRecords::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), HistoryRecords::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), HistoryRecords::getUrl, bo.getUrl());
        lqw.eq(bo.getLatitude() != null, HistoryRecords::getLatitude, bo.getLatitude());
        lqw.eq(bo.getLongitude() != null, HistoryRecords::getLongitude, bo.getLongitude());
        lqw.between(bo.getBeginTime() != null && bo.getEndTime() != null, HistoryRecords::getCreateTime, bo.getEndTime(), bo.getBeginTime());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HistoryRecords::getCreateByName, bo.getCreateByName());
        lqw.eq(bo.getCreateUserId() != null, HistoryRecords::getCreateUserId, bo.getCreateUserId());
        lqw.orderByDesc(HistoryRecords::getCreateTime);
        return lqw;
    }

    /**
     * 新增地图预警
     */
    @Override
    public Boolean insertByBo(HistoryRecordsBo bo) {
        HistoryRecords add = BeanUtil.toBean(bo, HistoryRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改地图预警
     */
    @Override
    public Boolean updateByBo(HistoryRecordsBo bo) {
        HistoryRecords update = BeanUtil.toBean(bo, HistoryRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HistoryRecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地图预警
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
